package mango.web.client.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mango.web.client.C;
import mango.web.client.View;

import com.google.gwt.dom.client.Style.BorderStyle;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class AdminContactView extends View {
	protected static AdminContactView adminContactView;

	public static AdminContactView get(String key) {
		if ("".equals(key)) {
			if (AdminContactView.adminContactView == null) AdminContactView.adminContactView = new AdminContactView().init();
			return AdminContactView.adminContactView;
		}
		return null;
	}

	public DialogBox dialogBox;
	public FlexTable tableContactHeader;
	public FlexTable tableContact;

	public AdminContactView init() {

		this.panel.setSize("1000px", "570px");

		AbsolutePanel panel;
		Label label;
		HTML html;
		TextBox textBox;
		CheckBox checkBox;
		ListBox listBox;
		RadioButton radioButton;
		TextArea textArea;
		Button button;
		int top;
		int left;

		panel = new AbsolutePanel();
		panel.setSize("1000px", "570px");
		this.panel.add(panel, 0, 0);

		left = 967;

		html = new HTML("<img src='image/exit.png' style='cursor:pointer'/>");
		panel.add(html, left, 0);
		html.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});

		html = new HTML("<img src='image/export.png' style='cursor:pointer' title='Export records'/>");
		panel.add(html, left - 40, 0);
		html.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				AdminExportView.get("").reset();
				AdminExportView.get("").dialogBox.show();
			}
		});

		top = 0;
		left = 0;
		this.tableContactHeader = new FlexTable();

		html = new HTML("<b><u>No</u></b>");
		html.setWidth("30px");
		this.tableContactHeader.setWidget(0, 0, html);

		html = new HTML("<b><u>memberId</u></b>");
		html.setWidth("80px");
		this.tableContactHeader.setWidget(0, 1, html);

		html = new HTML("<b><u>member</u></b>");
		html.setWidth("150px");
		this.tableContactHeader.setWidget(0, 2, html);

		html = new HTML("<b><u>contact</u></b>");
		html.setWidth("150px");
		this.tableContactHeader.setWidget(0, 3, html);

		html = new HTML("<b><u>email</u></b>");
		html.setWidth("200px");
		this.tableContactHeader.setWidget(0, 4, html);

		html = new HTML("<b><u>fax</u></b>");
		html.setWidth("100px");
		this.tableContactHeader.setWidget(0, 5, html);

		html = new HTML("<b><u>mobile</u></b>");
		html.setWidth("100px");
		this.tableContactHeader.setWidget(0, 6, html);

		panel.add(this.tableContactHeader, 0, 30);

		AbsolutePanel tablePanel;
		int width = 990;
		int height = 500;
		tablePanel = new AbsolutePanel();
		this.tableContact = new FlexTable();
		this.tableContact.setBorderWidth(1);
		tablePanel.getElement().getStyle().setOverflow(Overflow.AUTO);
		tablePanel.setSize(width + "px", height - 14 + "px");
		tablePanel.add(this.tableContact, 0, 2);
		panel.add(tablePanel, left, top + 30 + 16);

		// TODO:AdminSearchView
		this.dialogBox = new DialogBox();
		this.dialogBox.setHTML("<b style='color:red'>Searched Contacts</b>");
		this.dialogBox.setModal(false);
		this.dialogBox.setWidget(this.panel);

		return this;
	}

	public void setContactEmail(List<ContactVo> contactList) {

		tableRowRemove(tableContact);

		for (int i = 0; i < contactList.size(); i++) {
			ContactVo contactVo = contactList.get(i);
			EmailVo emailVo = C.getEmailVo(contactVo);

			HTML html = new HTML("" + (i + 1));
			html.setWidth(tableContactHeader.getWidget(0, 0).getElement().getStyle().getWidth());
			tableContact.setWidget(i, 0, html);

			html = new HTML("<a onclick='return false;'>" + emailVo.memberId + "</a>");
			html.setWidth(tableContactHeader.getWidget(0, 1).getElement().getStyle().getWidth());
			tableContact.setWidget(i, 1, html);
			html.getElement().setAttribute("memberId", emailVo.memberId + "");
			html.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					HTML htm = (HTML) event.getSource();
					AdminView.get("").gotoMember(htm.getElement().getAttribute("memberId"));
				}
			});

			html = new HTML(emailVo.memberHeader);
			html.setWidth(tableContactHeader.getWidget(0, 2).getElement().getStyle().getWidth());
			tableContact.setWidget(i, 2, html);

			html = new HTML(emailVo.contactHeader);
			html.setWidth(tableContactHeader.getWidget(0, 3).getElement().getStyle().getWidth());
			tableContact.setWidget(i, 3, html);

			html = new HTML(emailVo.email);
			html.setWidth(tableContactHeader.getWidget(0, 4).getElement().getStyle().getWidth());
			tableContact.setWidget(i, 4, html);

			html = new HTML(emailVo.fax);
			html.setWidth(tableContactHeader.getWidget(0, 5).getElement().getStyle().getWidth());
			tableContact.setWidget(i, 5, html);

			html = new HTML(emailVo.sms);
			html.setWidth(tableContactHeader.getWidget(0, 6).getElement().getStyle().getWidth());
			tableContact.setWidget(i, 6, html);

		}
	}

	public AdminContactView view() {

		return this;
	}
}
